﻿using System;
using System.Timers;

public class AlarmClock
{
    // 定义嘀嗒事件
    public event EventHandler<TickEventArgs> Tick;
    // 定义响铃事件
    public event EventHandler<AlarmEventArgs> Alarm;

    private Timer timer;
    private int alarmTime;
    private int currentTime = 0;

    public AlarmClock(int alarmTime)
    {
        this.alarmTime = alarmTime;
        timer = new Timer(1000); // 设置计时器每秒触发一次
        timer.Elapsed += Timer_Elapsed;
        timer.Start();
    }

    private void Timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        currentTime++;
        // 触发嘀嗒事件
        OnTick(new TickEventArgs(currentTime));

        // 检查是否到达设定的闹钟时间
        if (currentTime >= alarmTime)
        {
            // 触发响铃事件
            OnAlarm(new AlarmEventArgs("闹钟时间到！"));
            timer.Stop(); // 停止计时器
        }
    }

    protected virtual void OnTick(TickEventArgs e)
    {
        Tick?.Invoke(this, e);
    }

    protected virtual void OnAlarm(AlarmEventArgs e)
    {
        Alarm?.Invoke(this, e);
    }
}

// 定义嘀嗒事件参数类
public class TickEventArgs : EventArgs
{
    public int CurrentTime { get; }

    public TickEventArgs(int currentTime)
    {
        CurrentTime = currentTime;
    }
}

// 定义响铃事件参数类
public class AlarmEventArgs : EventArgs
{
    public string Message { get; }

    public AlarmEventArgs(string message)
    {
        Message = message;
    }
}









class Program
{
    static void Main(string[] args)
    {
        AlarmClock alarmClock = new AlarmClock(5); // 设定5秒后闹钟响起

        // 订阅嘀嗒事件
        alarmClock.Tick += (sender, e) =>
        {
            Console.WriteLine($"嘀嗒... 当前时间：{e.CurrentTime}秒");
        };

        // 订阅响铃事件
        alarmClock.Alarm += (sender, e) =>
        {
            Console.WriteLine(e.Message);
        };

        Console.ReadLine(); // 等待用户输入，保持程序运行
    }
}
























